home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / ITERATOR.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  2KB  |  88 lines

  1. #include "iterator.h"
  2. #include "collect.h"
  3. #include <stdlib.h>    // for exit
  4.  
  5. #define THIS    Iterator
  6. #define BASE    Object
  7. DEFINE_CLASS(Iterator,Object);
  8.  
  9. Iterator::Iterator(const Collection& c)
  10. {
  11.     cltn = (Collection*)&c;
  12.     cltn->doReset(*this);
  13. }
  14.  
  15. void Iterator::reset()
  16. {
  17.     cltn->doReset(*this);
  18. }
  19.  
  20. Object* Iterator::operator++()
  21. {
  22.     return ptr = cltn->doNext(*this);
  23. }
  24.  
  25. bool Iterator::operator==(const Iterator& a) const
  26. {
  27.     return cltn->isSame(*a.cltn) && index == a.index && num == a.num;
  28. }
  29.  
  30. const Class* Iterator::species() const
  31. {
  32.     return &class_Iterator;
  33. }
  34.  
  35. bool Iterator::isEqual(const Object& p) const
  36. {
  37.     return p.isSpecies(class_Iterator) && *this==*(Iterator*)&p;
  38. }
  39.  
  40. unsigned Iterator::hash() const
  41. {
  42.     return (unsigned)cltn ^ index ^ num;
  43. }
  44.  
  45. Object* Iterator::copy() const        { return shallowCopy(); }
  46.  
  47. void Iterator::deepenShallowCopy()
  48. {
  49. #if 1
  50.     cout << "error (gmv): Iterator::deepenShallowCopy() not implemented\n";
  51.     exit(1);
  52. //-gmv removed because of reference to class_SeqCltn:
  53. //Error iterator.cpp 85: Undefined symbol 'class_SeqCltn' in function Iterator::deepenShallowCopy
  54. #else
  55. // put back
  56. // Can only deepCopy() Iterators over SeqCltns
  57. // -- not Sets, Bags, Dictionaries, etc.
  58.     assertClass(*cltn,class_SeqCltn);
  59.     cltn = (Collection*)cltn->deepCopy();
  60.     if (index != 0) ptr = cltn->at(index-1);
  61. #endif
  62. }
  63.  
  64. Object* Iterator::shallowCopy() const
  65. {
  66. #if 1
  67.     cout << "error (gmv): Iterator::shallowCopy() not implemented\n";
  68.     exit(1);
  69.     return (Object*)0;
  70. //-gmv removed because of reference to class_SeqCltn:
  71. //Error iterator.cpp 94: Undefined symbol 'class_SeqCltn' in function Iterator::shallowCopy
  72. #else
  73. // Can only shallowCopy() Iterators over SeqCltns
  74. // -- not Sets, Bags, Dictionaries, etc.
  75.     assertClass(*cltn,class_SeqCltn);
  76.     Iterator* temp = (Iterator*)Object::shallowCopy();
  77.     temp->cltn = (Collection*)temp->cltn->copy();
  78.     if (index != 0) temp->ptr = temp->cltn->at(index-1);
  79.     return temp;
  80. #endif
  81. }
  82.  
  83. void Iterator::printOn(ostream& strm) const
  84. {
  85.     strm << className() << "(" << cltn->className()
  86.         << "[" << index << "]#" << num << ")";
  87. }
  88.